/**
 * 
 */
package fr.gfi.agraf.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author Fw
 * 
 */
public class VerificationUtils {

	/**
	 * Constructeur
	 */
	private VerificationUtils() {
		// Empty constructeur
	}

	/**
	 * Methode verifiant le regex d'un email
	 * 
	 * @param email
	 *            à checker
	 * 
	 * @return true si ok sinon false
	 */
	public static boolean isEmailValide(final String email) {
		final String emailPatern = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
				+ "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
		final Pattern pattern = Pattern.compile(emailPatern);
		final Matcher matcher = pattern.matcher(email);
		return matcher.matches();
	}

	/**
	 * Methode renvoyant false si les tableau sont null
	 * 
	 * @param allTabs
	 * @return
	 */
	public static boolean checkAreInputTabsOkNotNull(final Object[]... allTabs) {
		for (final Object obj : allTabs) {
			if (obj == null) {
				return false;
			}
		}
		return checkAreInputTabsOk(allTabs);
	}

	/**
	 * Verifie que tous les tableaux sont identiques (taille ou null)
	 * 
	 * @param allTabs
	 * @return
	 */
	public static boolean checkAreInputTabsOk(final Object[]... allTabs) {
		if (allTabs.length == 1) {
			return true;
		}
		boolean isTakeSize = false;
		int size = 0;
		int nbNull = 0;
		for (int i = 0; i < allTabs.length; i++) {
			final Object[] tab = allTabs[i];
			if (tab == null) {
				nbNull++;
				continue;
			}
			if (!isTakeSize) {
				size = tab.length;
				isTakeSize = true;
			} else {
				if (size != tab.length) {
					return false;
				}
			}
		}
		if (nbNull != 0 && nbNull != allTabs.length) {
			return false;
		}
		return true;
	}
}
